package com.engine.kq.cmd.dutyrules;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.biz.SimpleBizLogger;
import com.engine.common.constant.BizLogSmallType4Hrm;
import com.engine.common.constant.BizLogType;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 加班规则--删除
 */
public class DeleteDutyRulesCmd extends AbstractCommonCommand<Map<String, Object>> {

    private SimpleBizLogger logger;

    public DeleteDutyRulesCmd(Map<String, Object> params, User user) {
        this.user = user;
        this.params = params;
        this.logger = new SimpleBizLogger();

        String ruleIds = Util.null2String(params.get("ruleIds"));//需要删除的加班规则ID
        BizLogContext bizLogContext = new BizLogContext();
        bizLogContext.setLogType(BizLogType.HRM_ENGINE);//模块类型
        bizLogContext.setBelongType(BizLogSmallType4Hrm.HRM_ENGINE_KQ_OVERTIMERULES);//所属大类型
        bizLogContext.setLogSmallType(BizLogSmallType4Hrm.HRM_ENGINE_KQ_OVERTIMERULES);//当前小类型
        bizLogContext.setParams(params);//当前request请求参数
        logger.setUser(user);//当前操作人
        String mainSql = "select * from kq_dutyRules where (isDelete is null or isDelete<>1) and id in ("+ruleIds+")";
        logger.setMainSql(mainSql, "id");//主表sql
        logger.setMainPrimarykey("id");//主日志表唯一key
        logger.setMainTargetNameColumn("name");
        logger.before(bizLogContext);
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public List<BizLogContext> getLogContexts() {
        return logger.getBizLogContexts();
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String ruleIds = Util.null2String(params.get("ruleIds"));//需要删除的加班规则ID
        if (ruleIds.equals("")) {
            resultMap.put("status", "-1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(388858, user.getLanguage()));
            return resultMap;
        }

        boolean canDelete = HrmUserVarify.checkUserRight("KQOvertimeRulesDelete:Delete", user);//是否具有删除的权限;
        if (!canDelete) {
            resultMap.put("status", "-1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
            return resultMap;
        }

        boolean flag = false;
        String sql = "update kq_DutyRules set isDelete=1 where id in (" + ruleIds + ")";
        RecordSet recordSet = new RecordSet();
        flag = recordSet.executeUpdate(sql);
        if (flag) {
            resultMap.put("sign", "1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(20461, user.getLanguage()));//删除成功
        } else {
            resultMap.put("sign", "-1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(20462, user.getLanguage()));//删除失败
        }
        return resultMap;
    }
}
